from typing import List


class Solution:
    def longestOnes(self, nums: List[int], k: int) -> int:
        left = 0
        right = -1
        arr_len = len(nums)
        ret = 0
        while right + 1 < arr_len:
            if nums[right + 1] == 1:
                right += 1
                continue
            if nums[right + 1] == 0:
                if k > 0:
                    k -= 1 # change 0 to 1
                    right += 1
                    continue
                if k <= 0: # must shrink arr
                    ret = max(ret, right - left + 1)  # before shrink, update result
                    if nums[left] == 0:
                        ret = max(ret, right - left + 1)# before shrink, update result
                        left += 1
                        k += 1
                        continue
                    if nums[left] == 1:
                        left += 1
                        continue
        ret = max(ret, right - left + 1)
        return ret